/* Thumbs.java Copyright 2010 by Jo Pol
 *
 * This file is part of fs-thumbs.
 *
 * fs-thumbs  is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * fs-thumbs  is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BobbinWork.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.fs_thumbs;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.Arrays;

import com.googlecode.fs_thumbs.FsCollection.FsException;
import com.googlecode.fs_thumbs.FsCollection.Image;
import com.googlecode.fs_thumbs.FsCollection.Waypoint;

public class Main {

	/** tag for command line option */
	static final String CONFIG = "-config";

	/** tag for command line option */
	static final String VARIANT = "-variant";

	/** tag for command line option */
	static final String OUTPUT_DIRECTORY = "-dirout";

	protected int count = 0;
	protected final String mainHead;
	protected final String mainFoot;
	protected final String detailHead;
	protected final String detailFoot;
	protected final String linkToDetailsPage;
	protected final String imageLink;
	protected final String beforeImages;
	protected final String afterImages;
	private String dir;

	private String variant;

	final static String NL = System.getProperty("line.separator");

	public Main(final String[] args) throws FileNotFoundException, IOException {

		String configName = null;
		variant = null;
		for (int i = 0; i < args.length; i += 2) {
			if (args[i].equals(CONFIG)) {
				configName = args[i + 1];
			} else if (args[i].equals(VARIANT)) {
				variant = args[i + 1];
			} else if (args[i].equals(OUTPUT_DIRECTORY)) {
				dir = args[i + 1];
			}
		}
		if (variant == null)
			variant = "thumb";
		final Config config = new Config(configName);

		beforeImages = config.get(variant + ".before.images", "thumb.before.images", "dta.before.images");
		afterImages = config.get(variant + ".after.images", "thumb.after.images", "dta.after.images");
		imageLink = config.get(variant + ".image", "thumb.image", "dta.image");
		linkToDetailsPage = config.get(variant + ".link", "thumb.link", "dta.link");
		mainHead = config.get(variant + ".main.head", "thumb.main.head", "dta.main.head");
		mainFoot = config.get(variant + ".main.foot", "thumb.main.foot", "dta.main.foot");
		detailHead = config.get(variant + ".detail.head", variant + ".main.head", "thumb.detail.head", "thumb.main.head",
				"dta.detail.head", ".dta.main.head");
		detailFoot = config.get(variant + ".detail.foot", variant + ".main.foot", "thumb.detail.foot", "thumb.main.foot",
				"dta.detail.foot", ".dta.main.foot");
	}

	public static void main(final String[] args) throws FsException, FsException, IOException, FsException {

		if (args != null && args.length == 0) {
			throw new IllegalArgumentException("expected at least the url of a waypoint xml file");
		}
		final FsCollection fsCollection = new FsCollection(args[args.length - 1]);
		final Main main = new Main(args);
		if (main.variant.toLowerCase().endsWith("curl") || main.variant.toLowerCase().startsWith("curl")) {
			new CurlCommands(args, fsCollection);
		} else if (fsCollection.isLeaf()) {
			System.out.println(String.format(main.detailHead, htmlTitle(fsCollection)));
			main.printLeaf(fsCollection, System.out);
			System.out.println(MessageFormat.format(main.mainFoot, main.count));
		} else if (main.dir == null || main.dir == "") {
			new SinglePage(args, fsCollection);
		} else {
			new MultiPage(args, fsCollection, main.dir);
		}
	}

	protected FsCollection printLeaf(final FsCollection fsCollection, final PrintStream printStream) //
			throws FsException, FsException, IOException, FsException {

		printStream.print(crumbsAsHtmlHeaders(fsCollection));
		printStream.println(MessageFormat.format(beforeImages, //
				fsCollection.getChildCount(), fsCollection.getWaypointId(), fsCollection.getCollectionId()));
		for (final Image image : fsCollection.getImages()) {
			printStream.println(MessageFormat.format(imageLink, //
					image.getUrl(), image.getImageNumber(), image.getDgsNumber(), //
					fsCollection.getWaypointId(), fsCollection.getCollectionId(), image.getRow()));
			count++;
		}
		printStream.println(MessageFormat.format(afterImages, //
				fsCollection.getChildCount(), fsCollection.getWaypointId(), fsCollection.getCollectionId()));
		return fsCollection;
	}

	protected static String crumbsAsHtmlHeaders(final FsCollection fsCollection) throws FsException {
		StringBuffer result = new StringBuffer();
		int level = 1;
		for (final Waypoint crumb : fsCollection.getCrumbs()) {
			result.append(String.format("<h%1$d>%2$s</h%1$d>" + NL, level, crumb.getName()));
			level++;
		}
		result.append(String.format("<h%1$d>%2$s</h%1$d>", level, fsCollection.getName()));
		return result.toString();
	}

	protected static String htmlTitle(FsCollection fsCollection) throws FsException {
		String string = Arrays.toString(fsCollection.getCrumbs().toArray());
		String string2 = string.substring(1, string.length() - 1) + ", ";
		return string2.length() > 2 ? string2 : "" + fsCollection.getName();
	}
}
